//
//  BaseVC.m
//  itboyeios_gd
//
//  Created by Apple on 15/10/31.
//  Copyright © 2015年 Boye. All rights reserved.
//

#import "BaseVC.h"
#import "BaseNavVC.h"
#import "Reachability.h"

@interface BaseVC ()
{
    UIButton *_finishButton;
    
}
@property (nonatomic, strong) UIView *maskView;



@end

@implementation BaseVC

// 控制销毁的时候撤销移除通知的监听
- (void)dealloc {

//    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self registerLJWKeyboardHandler]; // 操作输入框移动
 
//    [self listenerNetworkStatus];

    // 键盘弹出通知
//    [self addKeyboardNotification];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    // 设置默认导航栏样式
    [self isNaviTranslucent:NO statesLightContent:YES];
    // 是首页的三个控制器则显示标签栏
    if ([self.navigationController.viewControllers indexOfObject:self] == 0) {
        // 退出的时候要显示导航栏
        MainTabBarController *tabBarController = (MainTabBarController *)self.navigationController.tabBarController;
        [tabBarController showTabBar];
    }
}
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated {
    
    _isModalBack = NO;
    
    [super viewWillDisappear:YES];
    if ([SVProgressHUD isVisible]) {
        [SVProgressHUD dismiss];
        self.view.userInteractionEnabled = YES;
    }
    
    [BoyeHttpClient cancelAllOperations];
}


/**
 *  导航栏样式设置，有透明和绿色两种
 *
 *  默认的样式是：白色状态栏，不透明导航栏
 *
 *  @param translucent 导航栏样式有透明和绿色两种
 *  @param light       状态栏颜色，YES则为白色，NO为黑色
 */
- (void)isNaviTranslucent:(BOOL)translucent statesLightContent:(BOOL)light {
    
    // 导航栏标题白色
    NSDictionary *attr = @{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:B_FONT(20)};
    [self.navigationController.navigationBar setTitleTextAttributes:attr];
    
    // 导航栏样式设置
    if (translucent) {
        if (!light) {
            [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
        } else {
            [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
        }
        self.navigationController.navigationBar.translucent = YES;
        self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:[UIColor clearColor]] forBarMetrics:UIBarMetricsDefault];
        self.navigationController.navigationBar.shadowImage = [UIImage imageWithColor:[UIColor clearColor]];
    } else {
        if (!light) {
            [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
        } else {
            [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
        }
        self.navigationController.navigationBar.translucent = NO;
        self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:SS_NAVI_BLUE_COLOR] forBarMetrics:UIBarMetricsDefault];
        self.navigationController.navigationBar.shadowImage = [UIImage imageWithColor:[UIColor clearColor]];
    }
}

#pragma mark - 导航栏左边返回按钮
// 左侧按钮的样式可以自定义，由imgname控制，如果填nil则默认是返回样式
- (void)createLeftBarButtonItemWithImagename:(NSString *)imgname {
    UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 25/2.0, 43/2.0)];
    [leftButton addTarget:self action:@selector(leftAction:) forControlEvents:UIControlEventTouchUpInside];
    // 如果穿的是nil，则是默认的返回样式按钮
    if (imgname == nil) {
        [leftButton setImage:[UIImage imageNamed:@"导航栏_返回"] forState:UIControlStateNormal];
    } else {
        [leftButton setImage:[UIImage imageNamed:imgname] forState:UIControlStateNormal];
    }
    UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = leftBarButton;
}

// 右侧按钮的样式可以自定义，由imgname控制，如果是文字则调用下面的接口
- (void)createRightBarButtonItemWithImagename:(NSString *)imgname {
    UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 25/2.0, 44/2.0)];
    [rightButton addTarget:self action:@selector(rightAction:) forControlEvents:UIControlEventTouchUpInside];
    [rightButton setImage:[UIImage imageNamed:imgname] forState:UIControlStateNormal];
    UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem = rightBarButton;
}

// 创建导航栏右侧的按钮，样式是文字的
- (void)createRightBarButtonItemWithTitle:(NSString *)title {
    UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 30)];
    [rightButton setTitle:title forState:UIControlStateNormal];
    [rightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    rightButton.titleLabel.font = FONT(15);
    rightButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [rightButton addTarget:self action:@selector(rightAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem = rightBarButton;
}


#pragma mark - 点击事件
// 导航栏左侧按钮事件
- (void)leftAction:(UIButton *)button {
    [self.navigationController popViewControllerAnimated:YES];
}
// 导航栏右侧按钮事件
- (void)rightAction:(UIButton *)button {
}

#pragma mark - 键盘弹出和下降相关设置
// 创建编辑完成后使键盘下降的按钮
- (void)_createFinishEditButton
{
    _finishButton = [[UIButton alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 30)];
    _finishButton.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
    _finishButton.layer.borderWidth = 0.5;
    _finishButton.layer.borderColor = [UIColor grayColor].CGColor;
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-60, 0, 60, 30)];
    titleLabel.text = @"完成";
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = BLUE_COLOR;
    [_finishButton addSubview:titleLabel];
    [_finishButton addTarget:self action:@selector(keyboardDown) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_finishButton];
}


// 键盘弹出和下降通知
- (void)addKeyboardNotification
{
    // 键盘弹出通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardFrameChange:) name:UIKeyboardWillShowNotification object:nil];
    // 键盘下降通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardFrameChange:) name:UIKeyboardWillHideNotification object:nil];
}



// 键盘弹出时执行，“完成”按钮跟随键盘一起上升
- (void)keyboardFrameChange:(NSNotification *)notification
{
    if (_finishButton == nil) {
        // 创建编辑完成后使键盘下降的按钮
        [self _createFinishEditButton];
    }
    [UIView animateWithDuration:0.15 animations:^{
        //获取键盘的frame
        NSValue *boundsValue = [notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
        CGRect frame = [boundsValue CGRectValue];
        //键盘y值
        CGFloat y = frame.origin.y;
        // "完成"按钮下降
        if (y < SCREEN_HEIGHT) {
            
            _finishButton.bottom = y - 64;
        }
        else {
            _finishButton.top = y;
        }
    }];
}

// 编辑完成按钮点击事件处理
- (void)keyboardDown
{
    // 使键盘下降
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
}



#pragma mark - 监听网络状态
- (void)listenerNetworkStatus
{
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    
    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        
        status = [AFNetworkReachabilityManager sharedManager].networkReachabilityStatus;
        
        switch (status) {
            case AFNetworkReachabilityStatusUnknown:
            {
                [SVProgressHUD showErrorWithStatus:@"您正在使用未知的网络!" withDuration:3];
            }
                break;
            case AFNetworkReachabilityStatusReachableViaWWAN:
            {
                if ([CommonCache shouldAlertWhenUseWWAN]) {
                    [SVProgressHUD showErrorWithStatus:@"您正在使用数据网络，请注意流量!" withDuration:3];
                    [CommonCache setNetworkStates:YES];
                }
            }
                break;
            case AFNetworkReachabilityStatusReachableViaWiFi:
                // 正在使用wifi
                break;
            case AFNetworkReachabilityStatusNotReachable:
            {
                if ([CommonCache shouldAlertWhenNetworkOff]) {
                    [SVProgressHUD showErrorWithStatus:@"网络已断开!" withDuration:3];
                    [CommonCache setNetworkStates:NO];
                }
            }
            default:
                break;
        }
    }];
    
}




#pragma mark - 遮罩视图
- (UIView *)maskView {
    
    if (_maskView == nil) {
        _maskView = [[UIView alloc] initWithFrame:self.view.bounds];
        _maskView.userInteractionEnabled = YES;
        [self.view addSubview:_maskView];
    }
    
    return _maskView;
    
}
- (void)addMask:(NSInteger)type {
    
    if (!(type == 0 || type == 1)) {
        return;
    }
    
    UIColor *bgColor;
    if (type == 0) {
        bgColor = [UIColor clearColor];
    } else {
        bgColor = [UIColor colorWithWhite:0.1 alpha:0.5];
    }
    self.maskView.backgroundColor = bgColor;
}
- (void)hideMask {
    [self.maskView removeFromSuperview];
    self.maskView = nil;
}



@end
